// pages/audio/audio.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    audioPath: '',
    time: 0
  },
  onUnload(){
    this.play.destroy();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.record = wx.getRecorderManager();
    this.play = wx.createInnerAudioContext();
    this.record.onStart(() => {
      wx.showToast({
        title: '开始录音',
      })
    })
    this.record.onStop((res) => {
      console.log(res)
      this.setData({
        audioPath: res.tempFilePath,
        time: res.duration
      })
    })

    this.play.onPlay(() => {
      wx.showToast({
        title: '开始播放',
      })
    })
    this.play.onPause(() => {
      wx.showToast({
        title: '暂停播放',
      })
    })
    this.play.onStop(() => {
      wx.showToast({
        title: '停止播放',
      })
    })
  },

  startRecord() {
    this.record.start({
      format: 'mp3'
    })
  },
  endRecord() {
    this.record.stop()
  },
  pauseRecord() {
    this.record.pause()
  },
  resumeRecord(){
    this.record.resume();
  },
  stopVoice(){
    this.play.stop();
  },
  pauseVoice(){
    this.play.pause();
  },
  playVoice(){
    this.play.src = this.data.audioPath;
    this.play.play();
  }
})